package group.igfd.manager.biz.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import group.igfd.manager.biz.dao.PublishExpertsDao;
import group.igfd.manager.biz.entity.PublishExpertsEntity;
import group.igfd.manager.biz.service.PublishExpertsService;


@Service("publishExpertsService")
public class PublishExpertsServiceImpl extends ServiceImpl<PublishExpertsDao, PublishExpertsEntity> implements PublishExpertsService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        List<String> paramOrderList = new ArrayList<>();
        paramOrderList.add("created_at");

        EntityWrapper<PublishExpertsEntity> publishExpertsEntityEntityWrapper = new EntityWrapper<>();
        String key = (String) params.get("key");
        if (StringUtils.isNotEmpty(key)) {
            publishExpertsEntityEntityWrapper.like("article","%" + key + "%");
        }
        String maxDate = (String) params.get("maxDt");
        if (StringUtils.isNotEmpty(maxDate)) {
            publishExpertsEntityEntityWrapper.le("created_at",maxDate);
        }

        String minDate = (String) params.get("minDt");
        if (StringUtils.isNotEmpty(minDate)) {
            publishExpertsEntityEntityWrapper.ge("created_at",minDate);
        }

        String state = (String) params.get("state");
        if (StringUtils.isNotEmpty(state)) {
            if ("0".equals(state)) {
                publishExpertsEntityEntityWrapper.eq("check_flag",0);
                publishExpertsEntityEntityWrapper.isNull("deleted_at");
            } else if ("1".equals(state)) {
                publishExpertsEntityEntityWrapper.eq("check_flag",1);
                publishExpertsEntityEntityWrapper.isNull("deleted_at");
            } else if ("2".equals(state)) {
                publishExpertsEntityEntityWrapper.isNotNull("deleted_at");
            } else if ("3".equals(state)) {
                publishExpertsEntityEntityWrapper.eq("check_flag",3);
                publishExpertsEntityEntityWrapper.isNull("deleted_at");
            }
        }

        publishExpertsEntityEntityWrapper.orderDesc(paramOrderList);

        Page<PublishExpertsEntity> page = this.selectPage(
                new Query<PublishExpertsEntity>(params).getPage(),
                //new EntityWrapper<PublishExpertsEntity>().like("article","%" + key + "%").orderDesc(paramOrderList)
                publishExpertsEntityEntityWrapper
        );

        return new PageUtils(page);
    }

}
